//
//  AppDelegate.m
//  Baidu
//
//  Created by C_HAO on 15/4/2.
//  Copyright (c) 2015年 C_HAO. All rights reserved.
//

#import "AppDelegate.h"
#import "Popover.h"
#import "StatusItemView.h"
#import "AboutPopover.h"
#import "PreferencePopover.h"
#import "UserPreferences.h"

@interface AppDelegate ()

@property (weak) IBOutlet NSMenu *menu;
@property NSStatusItem *statusItem;
@property NSWindow *window;
@property Popover *popover;
@property AboutPopover *aboutPopver;
@property PreferencePopover *preferencePopover;
@property StatusItemView *statusItemView;


@end

@implementation AppDelegate


- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    // Insert code here to initialize your application
    self.statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength];
    
    self.preferences = [[UserPreferences alloc] init];
    self.window = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, 1366, 768) styleMask:NSWindowStyleMaskBorderless backing:NSBackingStoreBuffered defer:YES screen:nil];
    
    self.statusItemView = [[StatusItemView alloc] initWithStatusItem:self.statusItem];
    self.statusItemView.image = [NSImage imageNamed:@"StatusIcon"];
    self.statusItemView.alternateImage = [NSImage imageNamed:@"StatusIcon_sel"];
    self.statusItemView.target = self;
    self.statusItemView.leftAction = @selector(mouseClick:);
    self.statusItemView.rightAction = @selector(rightClickStatusItem:);
    
    self.popover = [[Popover alloc] init];
    self.aboutPopver = [[AboutPopover alloc] init];
    self.preferencePopover = [[PreferencePopover alloc]init];
}


+ (AppDelegate *)appDelegate
{
    return (AppDelegate *)[NSApplication sharedApplication].delegate;
}

- (void)rightClickStatusItem:(id)sender
{
    [self.statusItem popUpStatusItemMenu:self.menu];
}

- (void)mouseClick:(id)sender
{
    [self.popover showPopover:sender];
    [NSApp activateIgnoringOtherApps:YES];
}

- (IBAction)preference:(id)sender
{
    [self.preferencePopover showPopover:self.statusItemView];
    [NSApp activateIgnoringOtherApps:YES];
}

- (IBAction)about:(id)sender
{
    [self.aboutPopver showPopover:self.statusItemView];
    [NSApp activateIgnoringOtherApps:YES];
}

- (IBAction)quit:(id)sender
{
    [NSApp terminate:sender];
}

@end
